#include "../include/LoopThreadPool.h"
#include "../include/EventLoop.h"
LoopThreadPool::LoopThreadPool(EventLoop *loop)
    : _thread_count(0),
      _next_loop_idx(0),
      _baseloop(loop)
{
}

void LoopThreadPool::setThreadCount(uint32_t threadcount) { _thread_count = threadcount; }

void LoopThreadPool::Create()
{
    if (_thread_count > 0)
    {
        _thread_pool.resize(_thread_count);
        _loops.resize(_thread_count);

        for (uint32_t i = 0; i < _thread_count; ++i)
        {
            _thread_pool[i] = new LoopThread();
            _loops[i] = _thread_pool[i]->getLoop();
        }
    }
}

EventLoop *LoopThreadPool::getNextLoop()
{
    if (_thread_count == 0)
        return _baseloop;
    _next_loop_idx = (_next_loop_idx + 1) % _thread_count;
    return _loops[_next_loop_idx];
}
